<template>
    <wl-model-form
            :show="show"
            :options="options"
            @result="formResult"
            @cancelShow="cancelShow"
            ref="wlModelForm">
    </wl-model-form>
</template>
<script>
import { resourceAction } from '@/api/google'

export default {
  name: 'WlResourceForm',
  props: {
    options: {
      type: Object,
      default: () => {
        return {}
      }
    },
    show: {
      type: Boolean,
      default: () => {
        return false
      }
    },
    filtrationFun: {} // 传递一个过滤表单数据的函数
  },
  data () {
    return {
      model: '',
      action: ''
    }
  },
  methods: {
    formResult (data) {
      let filtrationData = data
      if (typeof this.filtrationFun === 'function') {
        filtrationData = this.filtrationFun(data)
      }
      resourceAction(filtrationData, 'post', this.model, this.action).then(ret => {
        this.$refs.wlModelForm.loadingRun()
        this.$emit('result', ret.data)
      })
    },
    cancelShow () {
      this.$emit('cancelShow')
    }
  },
  mounted () {
    this.model = this.$route.params.model
    this.action = this.$route.params.action
  },
  watch: {
    $route (to) {
      this.model = to.params.model
      this.action = to.params.action
    }
  }
}
</script>
<style scoped lang="less">
    .wl-content {
        margin-bottom: 10px;

        &-group {
            margin: 10px;
        }

        &-button {
            margin-right: 10px;
        }
    }
</style>
